﻿using System;
using System.Text;

using Castle.MonoRail.Framework;

namespace StateTheater.Component
{
	public class ThumbBrowserComponent : ViewComponent
	{
		char selectedLetter;
		public override void Initialize()
		{
			base.Initialize();
			selectedLetter = (Context.ComponentParameters["selectedLetter"] as string ?? "A")[0];
			if (Char.IsDigit(selectedLetter))
				selectedLetter = '0';
		}

		public override void Render()
		{
			base.Render();
			RenderText(@"<table class=""Thumb"" >");
			RenderText("<tr>");
			StringBuilder line1 = new StringBuilder();

			foreach (char c in "0ABCDEFGHIJKLMNOPQRSTUVWXYZ")
			{
				if (c == selectedLetter)
				{
					line1.AppendFormat("<td align=\"center\" class=\"selectedThumb\"><A  href=\"list.rails?letter={0}\">{0}</A></td>\r\n", c);
				}
				else
				{
					line1.AppendFormat("<td align=\"center\" class=\"unselectedThumb\"><A  href=\"list.rails?letter={0}\">{0}</A></td>\r\n", c);
				}
			}

			RenderText("<tr>");
			RenderText(line1.ToString());
			RenderText(@"</tr>");

			RenderText("<tr>");
			RenderText("    <td class=\"leftThumb\">&nbsp;</td>");
			for (int i = 2; i < 27; ++i)
			{
				RenderText("    <td class=\"middleThumb\">&nbsp;</td>");
			}
			RenderText("    <td class=\"rightThumb\">&nbsp;</td>");
			RenderText(@"</tr>");

			RenderText(@"</table>");
			CancelView();
		}
	}
}
